﻿using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

using FarseerPhysics.Collision;
using FarseerPhysics.Common;

namespace Spacer
{
    public class PlanetMenu : Activity
    {
        public PlanetMenu(Planet planet)
        {
            dockedPlanet = planet;
            descriptions = new string[5] { "Planet Random Name\nPlanet Description goes here!", "Planet Herp Derp\nPlanet Description goes here!", "Planet Zebes\nPlanet Description goes here!", "Planet Omicron Persei 8\nPlanet Description goes here!", "Planet Stuff\nPlanet Description goes here!" };
			PlanetDisplaySize = new Vector2(Surface.Width * .7f, Surface.Height * .7f);
			UpgradesDisplaySize = new Vector2(Surface.Width * .6f, Surface.Height * .3f);
			ResourcesDisplaySize = new Vector2(Surface.Width * .4f, Surface.Height * .3f);
			InfoDisplaySize = new Vector2(Surface.Width - PlanetDisplaySize.X, Surface.Height * .7f);

			PlanetDisplayPosition = new Vector2(0f, 0f);
			UpgradesDisplayPosition = new Vector2(0, (int)PlanetDisplaySize.Y);
			ResourcesDisplayPosition = new Vector2((int)UpgradesDisplaySize.X, (int)PlanetDisplaySize.Y);
			InfoDisplayPosition = new Vector2((int)PlanetDisplaySize.X, 0f);
			selectionState = 0;

			selection2 = 0;
        }
        public void Initialize()
        {
            
        }

        public void Shutdown()
        {
        }

        public void Resumed()
        {
        }

        public void Paused()
        {
        }

        public void Update()
        {
            if (!m_bFinished && Input.IsKeyDown(Keys.Escape) || Input.IsGamePadButtonDown(GamePadButton.B))
                m_bFinished = true;

            foreach (Extractor e in dockedPlanet.Structures)
            {
                ExtractedResources = 0;
                ExtractedResources += e.ExtractedResources;
            }

			Ship ship = Globals.LocalPlayer.CurrentShip;

            DrawSelection2 = false;


            //bring to second selection menu
            if (selectionState == 1)
            {
                //built structures
                if (selection1 == 0)
                {
                    if (ship.DockedPlanet.Structures.Count == 0)
                    {
                        selection2 = 0;
                        selectionState = 0;

                    }
                    else
                        DrawSelection2 = true;
                    //Scroll through selections
                    if ((Input.IsKeyDown(Keys.Down) && !Input.WasKeyDown(Keys.Down)) ||
                        (Input.IsGamePadButtonDown(GamePadButton.Down) && !Input.WasGamePadButtonDown(GamePadButton.Down)))
                    {
                        selection2++;
                        if (selection2 >= ship.DockedPlanet.Structures.Count-1)
                            selection2 = 0;
                    }
                    else if ((Input.IsKeyDown(Keys.Up) && !Input.WasKeyDown(Keys.Up)) ||
                                (Input.IsGamePadButtonDown(GamePadButton.Up) && !Input.WasGamePadButtonDown(GamePadButton.Up)))
                    {
                        selection2--;
                        if (selection2 < 0)
                            selection2 = ship.DockedPlanet.Structures.Count-1;
                    }

                }
                //build structures
                if (selection1 == 1)
                {
                    DrawSelection2 = true;
                    if ((Input.IsKeyDown(Keys.Down) && !Input.WasKeyDown(Keys.Down)) ||
                        (Input.IsGamePadButtonDown(GamePadButton.Down) && !Input.WasGamePadButtonDown(GamePadButton.Down)))
                    {
                        selection2++;
                        if (selection2 >= 0)
                            selection2 = 0;
                    }
                    else if ((Input.IsKeyDown(Keys.Up) && !Input.WasKeyDown(Keys.Up)) ||
                                (Input.IsGamePadButtonDown(GamePadButton.Up) && !Input.WasGamePadButtonDown(GamePadButton.Up)))
                    {
                        selection2--;
                        if (selection2 < 0)
                            selection2 = 0;
                    }

                    if (((Input.IsKeyDown(Keys.Enter) && !Input.WasKeyDown(Keys.Enter)) ||
                        (Input.IsGamePadButtonDown(GamePadButton.A) && !Input.WasGamePadButtonDown(GamePadButton.A))) && selection2 == 0)
                    {
                        if (ship.Resources >= 2000)
                        {
                            ship.Resources -= 2000;
                            dockedPlanet.Structures.Add(new Extractor(dockedPlanet.World));
                        }

                    }

                }
                // buy items
                if (selection1 == 2)
                {
                    if (ship.DockedPlanet.Inventory.Count == 0)
                    {
                        selection2 = 0;
                        selectionState = 0;
                    }
                    else
                        DrawSelection2 = true;
                    if ((Input.IsKeyDown(Keys.Down) && !Input.WasKeyDown(Keys.Down)) ||
                        (Input.IsGamePadButtonDown(GamePadButton.Down) && !Input.WasGamePadButtonDown(GamePadButton.Down)))
                    {
                        selection2++;
                        if (selection2 > ship.DockedPlanet.Inventory.Count-1)
                            selection2 = 0;
                    }
                    else if ((Input.IsKeyDown(Keys.Up) && !Input.WasKeyDown(Keys.Up)) ||
                                (Input.IsGamePadButtonDown(GamePadButton.Up) && !Input.WasGamePadButtonDown(GamePadButton.Up)))
                    {
                        selection2--;
                        if (selection2 < 0)
                            selection2 = ship.DockedPlanet.Inventory.Count-1;
                    }
                    
                    //Removes the shield from the planet's inventory and adds it to the player
                    if (((Input.IsKeyDown(Keys.Enter) && !Input.WasKeyDown(Keys.Enter)) ||
                        (Input.IsGamePadButtonDown(GamePadButton.A) && !Input.WasGamePadButtonDown(GamePadButton.A))))
                    {
                        
                       if (ship.Resources >= ship.DockedPlanet.Inventory[selection2].ResourceWorth)
                        {
                                ship.Resources -= ship.DockedPlanet.Inventory[selection2].ResourceWorth;
                                ship.DockedPlanet.Inventory[selection2].GiveTo(ship);
                                ship.DockedPlanet.Inventory.RemoveAt(selection2);
                                

                        }  
                    }
                }

                //collect resources
                if (selection1 == 3)
                {
                    selectionState = 0;
                        foreach (Extractor e in dockedPlanet.Structures)
                        {
                            e.GiveResources(ship);
                            ExtractedResources -= e.ExtractedResources;
                        }

                }

                if ((Input.IsKeyDown(Keys.Left) && !Input.WasKeyDown(Keys.Left)) ||
                    (Input.IsGamePadButtonDown(GamePadButton.Left) && !Input.WasGamePadButtonDown(GamePadButton.Left)))
                {
                    selectionState = 0;
                }
            }



            //first selections
            if (selectionState == 0)
            {


                if ((Input.IsKeyDown(Keys.Down) && !Input.WasKeyDown(Keys.Down)) ||
                    (Input.IsGamePadButtonDown(GamePadButton.Down) && !Input.WasGamePadButtonDown(GamePadButton.Down)))
                {
                    selection1++;
                    if (selection1 >= 4)
                        selection1 = 0;
                }
                else if ((Input.IsKeyDown(Keys.Up) && !Input.WasKeyDown(Keys.Up)) ||
                         (Input.IsGamePadButtonDown(GamePadButton.Up) && !Input.WasGamePadButtonDown(GamePadButton.Up)))
                {
                    selection1--;
                    if (selection1 < 0)
                        selection1 = 3;
                }

                if ((Input.IsKeyDown(Keys.Enter) && !Input.WasKeyDown(Keys.Enter)) ||
                   (Input.IsGamePadButtonDown(GamePadButton.A) && !Input.WasGamePadButtonDown(GamePadButton.A)))
                {
                    selectionState = 1;
                }
            }
        }

        public void DrawWorld()
        {
        }

        public void DrawOverlay()
        {
            Surface.DrawColor = Color.Black;
            Surface.DrawRect(Vector2.Zero, new Vector2(Surface.Width, Surface.Height), Vector2.Zero, 0f, Layer.LAYER_20);

            DrawPrimaryMenu();
            DrawMenuItems();
            DrawDescription();
            //Displays how many resources are on the planet
            Surface.DrawString("Default", "Resources On Planet: " + dockedPlanet.Adamantium + 
                "\nExtracted Resources: " + ExtractedResources +
            "\nPlayer's Resources: " + Globals.LocalPlayer.CurrentShip.Resources, ResourcesDisplayPosition + ResourcesDisplaySize * .15f, Vector2.Zero, 0f);
            //Surface.DrawString("Default", "Player's Resources: " + (Globals.LocalPlayer.CurrentShip).Resources, ResourcesDisplayPosition + ResourcesDisplaySize * .25f, Vector2.Zero, 0f);
        }

        private void DrawPrimaryMenu()
        {
            Surface.DrawColor = Color.White;
            //Surface.DrawRect(dockedPlanet.Sprite., PlanetDisplayPosition+new Vector2(Surface.Width/2-InfoDisplaySize.X,0), new Vector2(Main.FindTexture("Planet").Width * (PlanetDisplaySize.X / PlanetDisplaySize.Y), Main.FindTexture("Planet").Height * (PlanetDisplaySize.X / PlanetDisplaySize.Y)), Vector2.Zero, 0f);
            Surface.DrawSprite(dockedPlanet.Sprite, PlanetDisplayPosition + new Vector2(PlanetDisplaySize.X - InfoDisplaySize.X, 250), new Vector2(255 * (PlanetDisplaySize.X / PlanetDisplaySize.Y), 255 * (PlanetDisplaySize.X / PlanetDisplaySize.Y)), Vector2.Zero, 0f, 0);
            
            /*Surface.DrawRect(Main.FindTexture("MiniMap"), UpgradesDisplayPosition, UpgradesDisplaySize, Vector2.Zero, 0f, Layer.LAYER_19);
            Surface.DrawRect(Main.FindTexture("MiniMap"), ResourcesDisplayPosition, ResourcesDisplaySize, Vector2.Zero, 0f, Layer.LAYER_19);
            Surface.DrawRect(Main.FindTexture("MiniMap"), InfoDisplayPosition, InfoDisplaySize, Vector2.Zero, 0f, Layer.LAYER_19);*/

			// Test - Looks way less stretched
			Surface.DrawNineSlice(Main.FindTexture("MiniMap"), UpgradesDisplayPosition, UpgradesDisplaySize, Layer.LAYER_19);
            Surface.DrawNineSlice(Main.FindTexture("MiniMap"), ResourcesDisplayPosition, ResourcesDisplaySize, Layer.LAYER_19);
            Surface.DrawNineSlice(Main.FindTexture("MiniMap"), InfoDisplayPosition, InfoDisplaySize, Layer.LAYER_19);
        }

        private void DrawMenuItems()
        {
			Ship ship = Globals.LocalPlayer.CurrentShip;

            float SingleItemWindowWidth = 20f / 7f;
            Vector2 StructureDisplayPos = new Vector2(UpgradesDisplaySize.X / 10 - SingleItemWindowWidth, Surface.Height - UpgradesDisplaySize.Y + 40);
            Vector2 StructureDisplaySize = new Vector2(SingleItemWindowWidth, UpgradesDisplaySize.Y);

            //Selection notification
            Surface.DrawColor = Color.Blue;
            Surface.DrawRect(new Vector2(StructureDisplayPos.X,StructureDisplayPos.Y+(selection1*30)), new Vector2(120, 20), Vector2.Zero, 0f, Layer.LAYER_1);
            
            //second section of the selection
            if(selectionState == 1 && DrawSelection2)
                Surface.DrawRect(new Vector2(StructureDisplayPos.X+160, StructureDisplayPos.Y + (selection2 * 30)), new Vector2(120, 20), Vector2.Zero, 0f, Layer.LAYER_1);
            //Draw the menu items

            Surface.DrawColor = Color.GreenYellow;
            Surface.DrawString("Default", "Built Structures", StructureDisplayPos, Vector2.Zero, 0f);
            Surface.DrawString("Default", "Build Structures", new Vector2(StructureDisplayPos.X, StructureDisplayPos.Y +30), Vector2.Zero, 0f);
            Surface.DrawString("Default", "Buy Items", new Vector2(StructureDisplayPos.X, StructureDisplayPos.Y + 60), Vector2.Zero, 0f);
            Surface.DrawString("Default", "Collect Resources", new Vector2(StructureDisplayPos.X, StructureDisplayPos.Y + 90), Vector2.Zero, 0f);

            if (selection1 == 0)
            {
                int i = 0;
                foreach (Item item in ship.DockedPlanet.Structures)
                {
                    Surface.DrawString("Default", item.Name, new Vector2(StructureDisplayPos.X + 160, StructureDisplayPos.Y+i), Vector2.Zero, 0f);
                    i+=30;
                }
            }
            if (selection1 == 1)
            {
                Surface.DrawString("Default", "Extractor", new Vector2(StructureDisplayPos.X + 160, StructureDisplayPos.Y), Vector2.Zero, 0f);
                //Surface.DrawString("Default", "Build Structures", new Vector2(StructureDisplayPos.X + 160, StructureDisplayPos.Y + 30), Vector2.Zero, 0f);
                //Surface.DrawString("Default", "Buy Items", new Vector2(StructureDisplayPos.X + 160, StructureDisplayPos.Y + 60), Vector2.Zero, 0f);
            }
            if (selection1 == 2)
            {
                foreach (Item item in dockedPlanet.Inventory)
                {
                    Surface.DrawString("Default", item.Name, new Vector2(StructureDisplayPos.X + 160, StructureDisplayPos.Y), Vector2.Zero, 0f);

                }
            }

            if (selection1 == 3)
            {
                int i = 0;
                foreach (Item item in ship.DockedPlanet.Structures)
                {
                    Surface.DrawString("Default", item.Name, new Vector2(StructureDisplayPos.X + 160, StructureDisplayPos.Y + i), Vector2.Zero, 0f);
                    i += 30;
                }
            }

        }

        private void DrawDescription()
        {
            Surface.DrawColor = Color.Yellow;
            Surface.DrawString("Default", descriptions[dockedPlanet.descriptionIndex], new Vector2(InfoDisplayPosition.X*1.1f,40), Vector2.Zero, 0f);
 
        }

        public bool IsModal()
        {
            return true;
        }

        public bool IsTransparent()
        {
            return true;
        }

        public bool IsFinished()
        {
            return m_bFinished;
        }

        private void UpdateMenu()
        {

        }

        private int selection1 { get; set; }
        private int selection2 { get; set; }
        private int selectionState { get; set; }
        private Galaxy galaxy { get; set; }
        private Planet dockedPlanet { get; set; }
       // private int selection { get; set; }
        bool m_bFinished;
        private int ExtractedResources { get; set; }
        private bool DrawSelection2 = true;

        private Vector2 PlanetDisplaySize;
        private Vector2 UpgradesDisplaySize;
        private Vector2 ResourcesDisplaySize;
        private Vector2 InfoDisplaySize;

        private Vector2 PlanetDisplayPosition;
        private Vector2 UpgradesDisplayPosition ;
        private Vector2 ResourcesDisplayPosition;
        private Vector2 InfoDisplayPosition;
        private string[] descriptions;
    }
}
